﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

namespace Jungle.Code.BookManager.WebControls
{
    public class BookListAdminPart: WebControl, IPostBackEventHandler
    {

        //------------------------------------------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));


            if (s1 == "delBook")
            {
                Book bToDel = new Book();
                bToDel.Id = Convert.ToInt32(s2);
                bToDel.Delete();                

            }
            if (s1 == "hideBook")
            {
                Book.HideUnHide(Convert.ToInt32(s2), false);
            }
            if (s1 == "unhideBook")
            {
                Book.HideUnHide(Convert.ToInt32(s2), true);
            }
        }
        //------------------------------------------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            #region BooksListTable


            writer.AddAttribute("width", "100%");
            writer.AddAttribute("height", "100%");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);


            writer.AddAttribute("bgcolor", "#CCCCCC");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr); //<tr>
            writer.AddAttribute("width", "30px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("ID");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Название");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("width", "70px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Категория");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("width", "70px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Цена");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("width", "70px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Действия");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag(); //</tr>

            Book Books = new Book();

            if (Context.Request["cmd"] != null && Context.Request["cmd"] == "view")
                Books.GetUnHidden();
            if (Context.Request["cmd"] != null && Context.Request["cmd"] == "viewHidden")
                Books.GetHidden();

            foreach (Book b in Books.BooksList) 
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr); //<tr>
                writer.AddAttribute("width", "30px");
                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(b.Id.ToString());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(b.Title.Trim());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(b.CategoryId.ToString().Trim());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(b.Price.ToString().Trim());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("class", "products_table_td");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_title");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                PrintBtnDel(writer, b.Id);
                PrintBtnEdit(writer, b.Id);
                if (Context.Request["cmd"] == "view") { PrintBtnHideUnhide(writer, b.Id, true); } else if (Context.Request["cmd"] == "viewHidden") { PrintBtnHideUnhide(writer, b.Id, false); }
                
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.RenderEndTag();
            }

            writer.RenderEndTag();

            #endregion
            base.RenderContents(writer);
        }
        //------------------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDel(HtmlTextWriter writer, int bookId)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "delBook," + bookId.ToString();

            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите удалить данную книгу? \"))" + cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Удалить");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_delete.jpg");
            return writer;
        }
        //------------------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnEdit(HtmlTextWriter writer, int bookId)
        {
            string linkStr = Context.Request.RawUrl;
            int t = linkStr.IndexOf("cmd");

            if (t != -1)
            {
                linkStr = linkStr.Substring(0, t + 4);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "edit&bId=" + bookId.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.AddAttribute("title", "Редактировать");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_edit.png");
            writer.RenderEndTag();
            return writer;
        }
        //------------------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnHideUnhide(HtmlTextWriter writer, int bookId, bool flagHideUnhide)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;

            if (flagHideUnhide)
            {
                opt.Argument = "hideBook," + bookId.ToString();
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите сделать товар недоступным для посетителей?\"))" + cs.GetPostBackEventReference(opt));
                writer.AddAttribute("title", "Убрать из каталога");
                writer.AddAttribute("style", "cursor: pointer;");
                PrintImage(writer, "/Admin/img/btn_blue_hide.jpg");
            }
            else
            {
                opt.Argument = "unhideBook," + bookId.ToString();
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите сделать товар доступным для посетителей?\"))" + cs.GetPostBackEventReference(opt));
                writer.AddAttribute("title", "Вернуть в каталог");
                writer.AddAttribute("style", "cursor: pointer;");
                PrintImage(writer, "/Admin/img/btn_blue_unhide.jpg");
            }
            return writer;
        }
        //------------------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //------------------------------------------------------------------------------------------------------

    }
}
